package com.example.example_20;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.io.BufferedReader;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.Buffer;

public class MainActivity extends AppCompatActivity {
    private Button btn1;
    private Button btn2;
    private String urladdress="http://v.juhe.cn/toutiao/";
    private String method="index";
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1=(Button) findViewById(R.id.btn1);
        btn1.setOnClickListener(new ButtonClickListener());

    }
    class ButtonClickListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            switch(v.getId()){
                case R.id.btn1:
                    doGet("8b13d671c5a2c4ffb94321ea8dcab509");
                    break;
                case R.id.btn2:
                    doPost("Girl");
                    break;
                default:
                    break;
            }

        }


    }
//doPost请求
    private void doPost(String s) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url=new URL(urladdress+method);
                    HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
                    httpURLConnection.setDoInput(true);
                    httpURLConnection.setDoOutput(true);
                    httpURLConnection.setRequestMethod("Post");
                    httpURLConnection.setUseCaches(false);
                    httpURLConnection.setRequestProperty("Accept-Charset","utf-8");
                    httpURLConnection.setRequestProperty("Content-Type","Application/x-www-form-urlencoded");
                    DataOutputStream outputStream=new DataOutputStream(httpURLConnection.getOutputStream());
                    final String content="sex="+s;
                    outputStream.writeBytes(content);
                    outputStream.flush();
                    outputStream.close();
                    if(httpURLConnection.getResponseCode()==200){
                        InputStream inputStream=httpURLConnection.getInputStream();
                        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
                        StringBuffer stringBuffer=new StringBuffer();
                        String reaLine="";
                        while((reaLine=bufferedReader.readLine())!=null){
                            stringBuffer.append(reaLine);
                        }
                        inputStream.close();
                        bufferedReader.close();
                        httpURLConnection.disconnect();
                        Log.d("TA",stringBuffer.toString());
                    }else{
                        Log.d("TA","failed!");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();


    }
//向服务器发送get请求
    private void doGet(String s) {
       final String getUrl=urladdress+method+"?key="+s;

        //final String getUrl="http://guolin.tech/api/china";
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL url=new URL(getUrl);
                    HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
                    httpURLConnection.connect();
                    if(httpURLConnection.getResponseCode()==200){
                        InputStream inputStream=httpURLConnection.getInputStream();
                        BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
                        StringBuffer stringBuffer=new StringBuffer();
                        String reaLine="";
                        while((reaLine=bufferedReader.readLine())!=null){
                            stringBuffer.append(reaLine);
                        }
                        inputStream.close();
                        bufferedReader.close();
                        httpURLConnection.disconnect();
                        Log.d("TAG",stringBuffer.toString());
                    }else{
                        Log.d("TAG","failed!");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}